package d0319;

public class Eaggtest2 {
    volatile boolean hasEaggs = false;
    Object flag=new Object();

    Thread human = new Thread(() -> {
        while (true) {
            if (!hasEaggs) {
                synchronized (flag) {
                    try {

                        System.out.println("等待");
                        flag.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                System.out.println("收取");
                hasEaggs = false;
            }
        }
    });


    Thread hen = new Thread(() -> {
        while (true) {
            try {
                Thread.sleep(300
                );
            } catch (InterruptedException e) {
                e.printStackTrace();

                hasEaggs = true;
                synchronized (flag){
                    notify();
                }
            }
        }
    });


    public static void main(String[] args) {
        Eaggtest2 eaggtest2 = new Eaggtest2();
        eaggtest2.hen.start();
        eaggtest2.human.start();
    }
}
